package com.fr.base.file;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.XMLFileManager;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.report.io.xml.SynchronizedVersion;
import com.fr.web.cluster.ClusterService;
import com.fr.web.core.service.ClusterUtilService;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;

public class ClusterConfigManager extends XMLFileManager
{
  private boolean isInit = false;
  private String localIp = null;
  private String localPort = null;
  private boolean useCluster = false;
  private List serviceList = new ArrayList();
  private ClusterService selfService = null;

  public String fileName()
  {
    return "cluster.xml";
  }

  public void initClusterServiceConfig(HttpServletRequest paramHttpServletRequest)
  {
    if (!(this.isInit))
    {
      int j;
      for (int i = 0; i < this.serviceList.size(); ++i)
        if (((ClusterService)this.serviceList.get(i)).isSelf())
          this.selfService = ((ClusterService)this.serviceList.get(i));
      if (this.selfService == null)
      {
        try
        {
          this.localIp = ClusterUtilService.getLocalIP().trim();
        }
        catch (Exception localException)
        {
          FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
        }
        this.localPort = String.valueOf(paramHttpServletRequest.getLocalPort()).trim();
        if ((StringUtils.isNotEmpty(this.localIp)) && (StringUtils.isNotEmpty(this.localPort)))
          for (j = 0; j < this.serviceList.size(); ++j)
          {
            ClusterService localClusterService = (ClusterService)this.serviceList.get(j);
            if ((this.localIp.equals(localClusterService.getIp())) && (this.localPort.equals(localClusterService.getPort())))
              this.selfService = localClusterService;
          }
      }
      this.isInit = true;
    }
  }

  public String getLocalIp()
  {
    return this.localIp;
  }

  public String getLocalPort()
  {
    return this.localPort;
  }

  public ClusterService getSelfService()
  {
    return this.selfService;
  }

  public List getOtherServices()
  {
    ArrayList localArrayList = new ArrayList();
    for (int i = 0; i < this.serviceList.size(); ++i)
      if ((this.selfService == null) || (!(this.selfService.equals(this.serviceList.get(i)))))
        localArrayList.add(this.serviceList.get(i));
    return localArrayList;
  }

  public ClusterService getService(String paramString)
  {
    if (paramString == null)
      return null;
    for (int i = 0; i < this.serviceList.size(); ++i)
      if (paramString.equals(((ClusterService)this.serviceList.get(i)).getServiceName()))
        return ((ClusterService)this.serviceList.get(i));
    return null;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    String str1;
    if (paramXMLableReader.isAttr())
    {
      if (this.serviceList == null)
        this.serviceList = new ArrayList();
      else
        this.serviceList.clear();
      if ((str1 = paramXMLableReader.getAttr("useCluster")) != null)
        setUseCluster(Boolean.valueOf(str1).booleanValue());
    }
    if (paramXMLableReader.isChildNode())
    {
      Object localObject;
      String str2 = paramXMLableReader.getTagName();
      if (str2.equals("XMLVersion"))
      {
        if ((str1 = paramXMLableReader.getElementValue()) != null)
        {
          localObject = SynchronizedVersion.getSynchronizedVersion(Thread.currentThread());
          ((SynchronizedVersion)localObject).setXmlVersionByString(str1);
        }
      }
      else if ("ClusterService".equals(str2))
      {
        localObject = new ClusterService();
        paramXMLableReader.readXMLObject((XMLReadable)localObject);
        this.serviceList.add(localObject);
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("ClusterConfig").attr("useCluster", this.useCluster);
    for (int i = 0; i < this.serviceList.size(); ++i)
      ((ClusterService)this.serviceList.get(i)).writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public boolean isUseCluster()
  {
    return this.useCluster;
  }

  public void setUseCluster(boolean paramBoolean)
  {
    this.useCluster = paramBoolean;
  }
}